#include "myhead.h"

void *pthread_show_lrc(void *arg)
{
    char s[1024];
    char file_name[1024];
    char name[1024] = {0};
    char tmp[1024];
    char *pos = NULL;
    float second = 0;
    int minute = 0;
    FILE *fp_lrc;    
    char r1[1024];
    char r2[1024];
    char current_line[1024] = {0};
    char next_line[1024] = {0};
    char next_next_line[1024] = {0};
    char prev_line[1024] = {0};
    char prev_prev_line[1024] = {0};

    sleep(1);
	pthread_detach(pthread_self());
    
    while(1)
    {   
        printf("[正在获取歌词]\n");
        printf("[正在获取歌词]\n");
        printf("[正在获取歌词]\n");
        pthread_mutex_lock(&mutex);
        while(mplayer_stop == 1);
        Send_Command("get_file_name\n");
        while(1)
        {
            memset(s,0,1024);
            memset(tmp,0,1024);
            memset(file_name,0,1024);
			printf("[%d]s = %s",__LINE__,s);
            fgets(s,1024,fp_mplayer);
            
            printf("[%d]s = %s",__LINE__,s);
            sscanf(s, "ANS_FILENAME='%s'",tmp);

            if(strlen(tmp) != 0)
            {
                break;
            }
        }
		
		pthread_mutex_unlock(&mutex);
		sprintf(s,"%c%d%c",'%',strlen(tmp)-1,'s');  // 格式化字符串，将文件名的长度写入 s 中

		memset(s,0,1024);  // 将数组 s 清零，初始化为空字符串
		sprintf(s,"%c%d%c",'%',strlen(tmp)-1,'s');  // 格式化字符串，将文件名的长度写入 s 中
		sscanf(tmp, s,file_name);  // 使用格式化字符串 s 解析 tmp 中的内容，提取出文件名并存储到 file_name 中

		memset(tmp,0,1024);  // 将数组 tmp 清零，初始化为空字符串
		// 在 file_name 中查找第一个出现的字符串 "." 的位置，并将其地址赋值给 pos
		pos=strstr(file_name, ".");  
		if(strcmp(pos,".avi")==0)
		{
			continue;  // 如果文件名以 ".avi" 结尾，则跳过本次循环，继续下一次迭代
		}

		if(strcmp(name,file_name) != 0)
		{
			if(strlen(name)==0)
			{
				memset(s,0,1024);  // 将数组 s 清零，初始化为空字符串
				sprintf(s,"lrc/%s.lrc",file_name);  // 构造歌词文件的路径，并存储到 s 中
				printf("歌词文件名：%s\n",s);
				fp_lrc = fopen(s,"r");  // 打开歌词文件
				if(fp_lrc == NULL)
				{
					perror("fopen fail");
					return NULL;
				}
			}
			 
			else
			{
				fclose(fp_lrc);  // 关闭已打开的歌词文件
				memset(s,0,1024);  // 将数组 s 清零，初始化为空字符串
				sprintf(s,"lrc/%s.lrc",file_name);  // 构造新的歌词文件路径，并存储到 s 中
				fp_lrc = fopen(s,"r");  // 打开新的歌词文件
				if(fp_lrc == NULL)
				{
					perror("fopen fail");
					return NULL;
				}
			}
		}
		else
        {
            fclose(fp_lrc);
            memset(s,0,1024);
            sprintf(s,"lrc/%s.lrc",file_name);
            fp_lrc = fopen(s,"r");
            if(fp_lrc == NULL)
            {
                perror("fopen fail");
                return NULL;
            }
        }
		
        strcpy(name,file_name);  // 将当前的文件名拷贝到变量 name 中

		memset(r1,0,1024);  // 将数组 r1 清零，初始化为空字符串
		memset(r2,0,1024);  // 将数组 r2 清零，初始化为空字符串
		ShowJpg(map,"bmp/lrc.jpg",0,0,1,1);  // 在显示器上显示指定的图片
		sprintf(r1,"%s",file_name);  // 格式化字符串，将文件名存储到 r1 中
		
		sscanf(r1, "%s.mp3\n", r1);
		printf("文件名:[%s]",r1);

		// 使用格式化字符串解析 r1，提取出不带扩展名的文件名，并存储回 r1 中
		/* char lrc_file_name[1024];
		sprintf(lrc_file_name, "%s.lrc", r1);
		sscanf(lrc_file_name, "%[^.]", r1);
		printf("解析文件名:[%s]",r1); */
		printf("开始播放歌词\n");  // 打印提示信息，表示开始播放歌词

		int minute_temp1 = 0, second_temp1 = 0;     //下一句的时间
		int minute_temp2 = 0, second_temp2 = 0;     //下下一句的时间
		int minute_temp3  = 0, second_temp3  = 0;   //缓存下下下一句的时间
		int k = 0;
        
        while(1)
		{
			if(k != 0)
			{
				// 清空数组并保存上一次和当前的歌词行
				memset(s, 0, 1024);
				memset(tmp, 0, 1024);
				strncpy(prev_prev_line, prev_line, sizeof(prev_prev_line));
				strncpy(prev_line, current_line, sizeof(prev_line));
				strncpy(current_line, next_line, sizeof(current_line));
				strncpy(next_line, next_next_line, sizeof(next_line));
				
				// 读取下下一句的歌词
				fgets(next_next_line, 1024, fp_lrc);
				sscanf(next_next_line, "[%d:%f]%s\n", &minute, &second, next_next_line);
				minute_temp3 = minute;
				second_temp3 = second;

				if(strlen(next_next_line) == 0)
				{
					// 如果下下一句歌词为空，则等待 `time_pos` 小于5后跳出循环
					while(time_pos > 5);
					//draw_line(50, 240, 400, 80, 0x00000000);
					break;
				}
			
				//sscanf(next_line, "[%d:%f]%s\n", &minute, &second, tmp);
				// 等待满足播放时机
				while((minute_temp1 * 60 + (int)second_temp1) > time_pos)
				{
					pthread_mutex_lock(&mutex);
					if(music_next == 1 || music_move == 1)
					{
						break;
					}
					pthread_mutex_unlock(&mutex);
				}
				// 检测是否需要切换歌曲或暂停播放
				if(music_next == 1 || music_move == 1)
				{
					pthread_mutex_unlock(&mutex);
					printf("[%d]005\n", __LINE__);
					break;
				}
				// 更新下一句和下下一句的时间
				second_temp1 = second_temp2;
				minute_temp1 = minute_temp2;
				second_temp2 = second_temp3;
				minute_temp2 = minute_temp3;
				

				// 读取下一句和下下一句的歌词
			
				ShowJpg(map,"bmp/lrc.jpg",0,0,1,1);
				Display_characterX(200,20,r1,0x00dbefff,2);
				
				Display_characterX(190, 90,prev_prev_line, 0x003ba7ff, 2);     //上上一句歌词
				Display_characterX(190, 130,prev_line, 0x0084c8ff,2);          //上一句歌词
				Display_characterX(130, 180,current_line, 0x00dbefff, 3);       //当前歌词
				Display_characterX(190, 250,next_line, 0x0084c8ff, 2);          //下一句歌词
				Display_characterX(190, 310,next_next_line, 0x003ba7ff, 2);     //下下一句
		 
				}
				//260 210 150
				/*初始化*/
				
				else
				{
				memset(s, 0, 1024);
				memset(tmp, 0, 1024);
				fgets(s, 1024, fp_lrc);
				if(strlen(s)==0)
				{
					while(time_pos>5);
					//draw_line(50, 240, 400, 80, 0x00000000);
					break;
				}
				sscanf(s, "[%d:%f]%s\n", &minute, &second, tmp);

				while((minute*60+(int)second) > time_pos)
				{
					pthread_mutex_lock(&mutex);
					if(music_next == 1 || music_move == 1)
					{
						break;
					}
					pthread_mutex_unlock(&mutex);
				}


				strncpy(prev_prev_line, prev_line, sizeof(prev_prev_line));
				strncpy(prev_line, current_line, sizeof(prev_line));
				strncpy(current_line, tmp, sizeof(current_line));
				//strcpy(next_line, nextlrc(fp_lrc));

				// 读取下一句和下下一句的歌词
				fgets(next_line, 1024, fp_lrc);
				sscanf(next_line, "[%d:%f]%s\n", &minute, &second, next_line);
				second_temp1 = second;
				minute_temp1 = minute;
				fgets(next_next_line, 1024, fp_lrc);
				sscanf(next_next_line, "[%d:%f]%s\n", &minute, &second, next_next_line);
				second_temp2 = second;
				minute_temp2 = minute;


				ShowJpg(map,"bmp/lrc.jpg",0,0,1,1);
				Display_characterX(200,20,r1,0x00dbefff,3);
				
				Display_characterX(190, 90, prev_prev_line, 0x003ba7ff, 2);     //上上一句歌词
				Display_characterX(190, 130, prev_line, 0x0084c8ff, 2);          //上一句歌词
				Display_characterX(130, 180, current_line, 0x00dbefff, 3);       //当前歌词
				Display_characterX(190, 250, next_line, 0x0084c8ff, 2);          //下一句歌词
				Display_characterX(190, 310, next_next_line, 0x003ba7ff, 2);     //下下一句                  
				k = 1;
				}
				printf("clean = %d",clean);
				if(clean == 1)
				{
					clean = 0;
					printf("退出\n");
					break;
				}

			}
			printf("即将刷新\n");
			sleep(3);
			printf("刷新成功\n");
			memset(name, 0, 1024);
			memset(current_line, 0, 1024);
			memset(next_line, 0, 1024);
			memset(next_next_line, 0, 1024);
			memset(prev_line, 0, 1024);
			memset(prev_prev_line, 0, 1024);
		
		}
    
    return NULL;
}